<template>
	<view class="main">
		<view class="top">

			<view class="ta">
				<view class="x" style="margin: 0px 16px;">项目</view>

          <view class="opt"
          	style="background-color: #FFF;border: #e7e6e4 0.5px solid;border-radius: 5px;">
            <view class="xmmc" @click="getProject()"
            	style="padding: 9px 9px;display: flex;justify-content: space-between;align-items: center;">
            	<view class="opt1" v-show="proTxt=='请选择'" style="font-size: 15px;">
            		{{'请选择'}}
            	</view>
            	<view class="opt2" v-show="proTxt!='请选择'">
            		{{proTxt}}
            	</view>
            	<view class="opt3">
            		<u-icon name="arrow-down" style="margin-top: 3px;"></u-icon>
            	</view>
            </view>
          </view>
          
<!-- 					<u-text :text="proTxt" @click="pro()"></u-text>
					<view class="right-zzzz" style="margin-left: 10px;">
						<u-icon name="arrow-down" @click="pro()"></u-icon>
						<u-picker :defaultIndex="[getDefaultPro()]" :show="proShow" :columns="proListx" keyName="xmmc"
							@confirm="changePro" closeOnClickOverlay @close="proCancel()"
							@cancel="proCancel()"></u-picker>
					</view> -->

			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div>
			
			<view class="ta">
				<view class="x" style="margin: 0px 16px;">建设状态</view>
				<view class="ayxzzz">
					<u-text :text="jsztTxt" @click="jszt()"></u-text>
					<view class="right-zzzz" style="margin-left: 10px;">
						<u-icon name="arrow-down" @click="jszt()"></u-icon>
						<u-picker :defaultIndex="[getDefaultJszt()]" :show="jsztShow" :columns="jsztList" keyName="name"
							@confirm="changeJszt" closeOnClickOverlay @close="jsztCancel()"
							@cancel="jsztCancel()"></u-picker>
					</view>
				</view>
			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div>
			
			<view class="ta short">
				<view class="x" style="margin: 0px 16px;">项目进度</view>
				<u--input placeholder="请输入" v-model="xmjdTxt" style="background-color: #FFF;margin: 10px 16px 0px 14px;">
          <template slot="suffix">%</template>
        </u--input>
			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div>
			
			<view class="ta">
				<view class="x" style="margin: 0px 16px;">关联项目巡查单</view>
				<view class="ayxzzz">
					<u-text :text="xmxcdTxt" @click="xmxcd()"></u-text>
					<view class="right-zzzz" style="margin-left: 10px;">
						<u-icon name="arrow-down" @click="xmxcd()"></u-icon>
					</view>
				</view>
			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div>
      
      <view class="ta">
      	<view class="x" style="margin: 0px 16px;">历史巡查日志单</view>
      	<view class="ayxzzz">
      		<u-text :text="xcrzTxt" @click="xcrzd()"></u-text>
      		<view class="right-zzzz" style="margin-left: 10px;">
      			<u-icon name="arrow-down" @click="xcrzd()"></u-icon>
      		</view>
      	</view>
      </view>
      <div style="margin: 16px 0px;"><u-line></u-line></div>
			
			<view class="xxxdas" style="color: #999;">
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">项目开始日期</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.startDate) ? proDetail.startDate : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
				
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">项目结束日期</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.endDate) ? proDetail.endDate : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
				
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">责任部门/区县</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.trbm) ? proDetail.trbm : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
				
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">责任人姓名</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.zrrxm) ? proDetail.zrrxm : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
				
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">联系方式</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.lxfs) ? proDetail.lxfs : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
				
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">设计单位</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.sjdw) ? proDetail.sjdw : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
				
				<view class="ta">
					<view class="x" style="margin: 0px 16px;">施工单位</view>
					<view class="ayxzzz" style="min-height: 20px;">
						{{ (proDetail && proDetail.sgdw) ? proDetail.sgdw : '' }}
					</view>
				</view>
				<div style="margin: 16px 0px;"><u-line></u-line></div>
			</view>

		</view>
		<view class="content">
      
<!-- 			<view class="ca" style="margin: 0px 16px;">
				<view class="x">巡查地点</view>
				<view class="ae">
					<u--textarea v-model="xcddTxt" count height="60px" maxlength='1000'
						style="margin-top: 16px;"></u--textarea>
				</view>
			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div> -->
      <view class="tc" style="margin: 0px 16px;">
      	<view class="x">巡查地点</view>
      	<view class="cy" style="margin-top: 16px;">
      		<u-input v-model="xcddTxt" style="background-color: #FFF;" readonly @click.native="location(true)">
      			<template slot="suffix">
      				<u-icon name="map" @click="location(true)"></u-icon>
      			</template>
      		</u-input>
      	</view>
      </view>
      <div style="margin: 16px 0px;"><u-line></u-line></div>
      
      <view class="ta">
      	<view class="x" style="margin: 0px 16px;">巡查人员</view>
        <u--input placeholder="支持录入多个人名(用,隔开)" v-model="xcry" style="background-color: #FFF;margin: 10px 16px 0px 14px;"></u--input>
      </view>
      <div style="margin: 16px 0px;"><u-line></u-line></div>
			
			<view class="ca" style="margin: 0px 16px;">
				<view class="x">存在问题/现场施工情况</view>
				<view class="bnxxx" v-for="(work,indexW) in questionList" :key="indexW">
					<view class="ae" style="display: flex;justify-content: space-between;align-items: flex-end;">
						<u--textarea v-model="work.content" count height="60px" maxlength='2000'
							style="margin-top: 16px;flex: 1;"></u--textarea>
						<u-icon name="trash" size="20" style="margin-left: 5px;margin-bottom: 1px;" @click="deleteQuestion(indexW)"></u-icon>
					</view>
				</view>
				<view class="sxnnnn" style="display: flex;justify-content: center;margin-top: 16px;">
					<u-icon name="plus-circle" color="#FF5500" size="22" @click="addQuestion"></u-icon>
				</view>
			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div>

			<view class="cb" style="margin: 0px 16px;">
				<view class="x">现场照片</view>
				<view class="be"
					style="border: 1px solid #ceccca;height: 72px;margin-top: 16px;display: flex;border-radius: 4px;">
					<view class="aaa" style="display: flex;" v-for="(item,index) in fileTempList" :key="index">
						<view style="position: relative; margin-top: 8px; margin-left: 8px;">
							<u--image :src="item.url" width="55px" height="55px"></u--image>
							<u-icon name="close" style="position: absolute; top: 2px; right: 2px;"
								@click="removeItem(index)"></u-icon>
						</view>
					</view>
					<u--image :src="upLoadUrl" @click="getFile" v-show="fileTempList.length<5" width="55px"
						height="55px" style="margin-top: 8px;margin-left: 8px;"></u--image>
				</view>
			</view>
			<div style="margin: 16px 0px;"><u-line></u-line></div>
			
			<view class="ta">
				<view class="x" style="margin: 0px 16px;">是否整改完成</view>
				<view class="ayxzzz">
					<u-text :text="zgTxt" @click="zg()"></u-text>
					<view class="right-zzzz" style="margin-left: 10px;">
						<u-icon name="arrow-down" @click="zg()"></u-icon>
						<u-picker :defaultIndex="[getDefaultZg()]" :show="zgShow" :columns="zgList" keyName="name"
							@confirm="changeZg" closeOnClickOverlay @close="zgCancel()"
							@cancel="zgCancel()"></u-picker>
					</view>
				</view>
			</view>
			
		</view>
		<view class="bottom" style="margin: 24px 16px 0px 16px;">
			<u-button @tap="submit" text="提 交" color="#FF5000" style="border-radius: 4px;" :loading="loading"></u-button>
			<u-button @click="reset" text="重 置" color="#FF5000" plain
				style="margin-top: 8px;border-radius: 4px;"></u-button>
		</view>
		<div style="height: 16px;"></div>

		<u-popup :show="xmxcdShow" closeOnClickOverlay round="10" customStyle="min-height:40vh" @close="xmxcdShow = false">
			<view class="xmxcpopup" style="margin: 16px;">
				<view class="top" style="font-size: 16px;color: #000;text-align: center;margin-bottom: 10px;">
					关联项目巡查单
				</view>
				<view class="choice_item_xmxc">
					<view class="no_choice" v-show="xmxcList.length==0">
						<u-empty mode="search" text="暂无数据" style="margin-top: 10vh;"></u-empty>
					</view>
					<view class="has_data" v-show="xmxcList.length != 0">
						<view class="data_container_xmxc" v-for="(xp,indexP) in xmxcList" :key="indexP">
							<view class="xmxc_item" style="display: flex;align-items: center;justify-content: space-between;color: #333;font-size: 14px;padding: 10px 0px;">
								<view class="left" style="display: flex;align-items: center;">
									<view class="choice_item" style="margin-right: 5px;">
										<view class="choice_a" v-show="xmxcdTxt!=xp.yjbh" @click="choiceThisItem(xp)">
											<view class="xxxxxxxxx" style="height: 16px;width: 16px;border: 1px solid #999;border-radius: 20px;"></view>
										</view>
										<view class="choice_b" v-show="xmxcdTxt==xp.yjbh" @click="xmxcdShow = false">
											<u-icon name="checkmark-circle-fill" color="#FF5500" size="20"></u-icon>
										</view>
									</view>
									<view class="choice_name">
										{{xp.yjbh}}
									</view>
								</view>
								<view class="center">
									{{getZt(xp)}}
								</view>
								<view class="right" style="color: #2979ff;" @click="jumpToDetailXmxcd(xp)">
									详情
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		
    <!-- 历史巡查日志单 -->
    <u-popup :show="xcrzShow" closeOnClickOverlay round="10" customStyle="min-height:40vh" @close="xcrzShow = false">
    	<view class="xmxcpopup" style="margin: 16px;">
    		<view class="top" style="font-size: 16px;color: #000;text-align: center;margin-bottom: 10px;">
    			历史巡查日志单
    		</view>
    		<view class="choice_item_xmxc">
    			<view class="no_choice" v-show="xcrzList.length==0">
    				<u-empty mode="search" text="暂无数据" style="margin-top: 10vh;"></u-empty>
    			</view>
    			<view class="has_data" v-show="xcrzList.length != 0">
    				<view class="data_container_xmxc" v-for="(xp,indexP) in xcrzList" :key="indexP">
    					<view class="xmxc_item" style="display: flex;align-items: center;justify-content: space-between;color: #333;font-size: 14px;padding: 10px 0px;">
    						<view class="left" style="display: flex;align-items: center;">
    							<view class="choice_item" style="margin-right: 5px;">
                    （{{ indexP + 1}}）
    							</view>
    							<view class="choice_name">
    								{{xp.createdTime.slice(0, 10)}}
    							</view>
    						</view>
    						<view class="center">
    							{{xp.xcry}}
    						</view>
    						<view class="right" style="color: #2979ff;" @click="jumpToDetail2(xp)">
    							详情
    						</view>
    					</view>
    				</view>
    			</view>
    		</view>
    	</view>
    </u-popup>
    
    <!-- 项目 -->
    <u-popup :show="pshow" :round="10" mode="bottom" @close="pshow=false">
    	<view style="height: 60vh;">
    		<view class="ptop" style="margin: 10px;">
    			<u-search v-model="keyword" @search="getprojectList()" @custom="getprojectList()"
    				:clearabled="false"></u-search>
    		</view>
    		<u-list height="52vh">
    			<u-list-item v-for="(item, index) in proListx" :key="index">
    				<u-cell @click="chooseproject(item)">
    					<view slot="title" class="cleft" style="width: 80vw;">
    						{{ item.xmmc }}
    					</view>
    					<view slot="right-icon" class="aright" v-show="proTxt==item.xmmc">
    						<u-icon name="checkmark" color="#FF5500"></u-icon>
    					</view>
    				</u-cell>
    			</u-list-item>
    		</u-list>
    	</view>
    </u-popup>

    <!-- 定位地图图层 -->
    <u-popup :show="showMap" mode="center" @close="showMap=false" :round="10" :safeAreaInsetBottom="false">
    	<view class="content" style="width: 90vw;height: 90vh;display: flex;flex-direction: column;">
    		<view class="top" style="flex: 1;">
    			<view class="map" style="height: 100%;width: 100%;" @touchend="handleTouchEnd">
    				<comp-map ref="mainMap" text="1" @mapMounted="onMapMpunted" :showLoc="false"
    					:showGol="false"></comp-map>
    			</view>
    			<u-icon name="map" size="40" color="#FF5000"
    				style="position: absolute; bottom: 50%; left: 50%; transform: translate(-50%, 50%);padding-bottom: 148px;z-index: 1000;"></u-icon>
    		</view>
    		<view class="bottom" style="height: 110px;">
    			<view class="atop"
    				style="margin:3px;display: flex;justify-content: space-between;align-items: center;height: 50px;">
    				<u-icon name="map" style="margin-left: 5px;"></u-icon>
    				<u-text :text="tempLocTxt" style="margin-left: 5px;"></u-text>
    			</view>
    			<div><u-line></u-line></div>
    			<view class="bbottom" style="display: flex;height: 45px;">
    				<view class="a" @click="upLocation()"
    					style="width: 50%;display: flex;justify-content: center;align-items: center;">
    					<view style="color: #FF5000;">确 认</view>
    				</view>
    				<div><u-line direction="col"></u-line></div>
    				<view class="b" @click="cancel()"
    					style="width: 50%;display: flex;justify-content: center;align-items: center;">
    					<view style="color: #909399;">取 消</view>
    				</view>
    			</view>
    		</view>
    	</view>
    </u-popup>
	</view>
</template>

<script>
	import croods from '@/common/croods-1.3.3.js';
	window.croods = croods;
	window.hydra = require('../../../common/wst_hydra-1.0.0.js');
  import compMap from "@/components/comp-map/comp-map.vue"
	import coordtransform from 'coordtransform';
	import {
		getAllProject,
    getAllProjectNew,
    getProjectDetail,
		getDateDic,
		getXmMessage,
		uploadFile,
		postXcrz,
		proList,
    getPatrolLog,
    getPatrolDetail
	} from '@/common/api.js'
	export default {
    onLoad: function(option){
      if(option.id) {
        this.planId = option.id
        this.chooseproject({id:option.xmid,xmmc:option.xmmc})
      }
    },
		onShow() {
			document.body.style.overflow = 'auto'
			this.getDic(this.jsztreq)
			this.getDic2()
			this.getAllProject()
      this.location(false)
		},
    components: {
    	compMap
    },
		data() {
			return {
				upLoadUrl: require("@/static/icons/xjyw/add.png"),
				showMap: false,
        planId: '',
				/* 项目请求体 */
				query: {
					id: 0,
					ssdfq: '',
					xmlb: '',
					xmmc: ''
				},
        pshow: false,
				proListx:[],
        keyword: '',
				proShow: false,
				proStatus: 0,
				proTxt: '请选择',
				proId:null,
				proDetail:{},
				
        // 巡查日志
        xcrzTxt: '请选择查看',
        xcrzShow: false,
        xcrzId: null,
        xcrzList: [],
				/* 项目巡查单 */
				xmxcdTxt:'请选择',
				xmxcdShow:false,
				xmxcdId:null,
				xmxcList:[],
				
				/* 建设状态 */
				jsztreq:{
					status: 'Enabled',
					type: 'PROJECT_STAGE'
				},
				jsztList:[],
				jsztShow:false,
				jsztStatus:0,
				jsztTxt: '请选择',
				jsztCode:'',
				
				/* 整改状态 */
				zgList:[
					[
						{name:'是',value:1},
						{name:'否',value:0}
					]
				],
				zgShow:false,
				zgStatus:0,
				zgTxt:'请选择',
				zgValue:null,
				zgDicList:[],
				
				/* 存在问题 */
				questionList:[
					{content : ''}
				],

				/* 请求体参数 */
				xmjdTxt:'',
				xcddTxt: '',
        xcddPos: '',
				fileNum: null,

				/* 图片暂存列表 */
				fileTempList: [],
				/* 图片格式 */
				ijpeg: 'image/jpeg',
				ipng: 'image/png',
				patrolList: [],// 巡查日志
        xcry: '', // 巡查人员
        
        /* 临时定位变量 */
        tempLat: null,
        tempLng: null,
        tempLocTxt: '',
        
        /* 当前定位经纬度 */
        locLat: null,
        locLng: null,
        tk: '033b2d353901718af77cac1bc9a85c6e',
        loading: false,
        submited: false
			}
		},
		methods: {
			/* 获取全部项目 */
			getAllProject(){
				getAllProjectNew(this.query).then(res => {
					this.proListx = res
				})
			},
      getProject() {
      	this.pshow = true
      },
      getprojectList() {
      	this.query.xmmc = this.keyword
      	getAllProjectNew(this.query).then(res => {
      		this.proListx = res
      	})
      },
      chooseproject(item) {
        this.proTxt = item.xmmc
        this.proId = item.id
        this.proStatus = 1
      	getProjectDetail({
      		id: item.id
      	}).then(res => {
          this.pshow = false
          this.proDetail.startDate = res.startDate
          this.proDetail.endDate = res.endDate
          this.proDetail.trbm = res.trbm
          this.proDetail.zrrxm = res.zrrxm
          this.proDetail.lxfs = res.lxfs
          this.proDetail.sjdw = res.sjdw
          this.proDetail.sgdw = res.sgdw
          this.xmjdTxt = res.shjsjd
          if(res.shjszt) {
            this.jsztTxt = this.jsztList[0].find(item => item.code == res.shjszt).name
            this.jsztCode = res.shjszt
          }
      	})
        this.getXcproList(item.id)
        this.getPatrolLog()
      },
      // 历史巡查日志
      getPatrolLog() {
        getPatrolLog({
          pageNum: 1,
          pageSize: 999,
          xmid: this.proId
        }).then(res => {
          this.xcrzList = res.list
        })
      },
      location(showMap) {
      	let that = this
      	window.croods.customPlugin({
      		action: 'ServicePermissionPlugin.getLocation',
      		params: {
      			reason: '获取定位信息',
      			required: 1,
      			permissionPurpose: '获取位置信息',
      			coorType: 'GCJ02'
      		},
      		success: function(res) {
      			let wgs84Coords = coordtransform.gcj02towgs84(res.longitude, res.latitude);
      			that.locLng = wgs84Coords[0]   
      			that.locLat = wgs84Coords[1]
      			that.fswzTxt = res.addrStr
      			that.tempLocTxt = res.addrStr
      	    that.xcddTxt = res.addrStr
      	    that.xcddPos = `${that.locLng},${that.locLat}`
      			that.showMap = showMap
      		},
      		fail: function(msg) {
      			that.$u.toast(msg);
      		}
      	});
      },
      /* 地图初始定位 */
      async onMapMpunted() {
      	try {
      		let temp = {
      			lng: this.locLng,
      			lat: this.locLat
      		}
      		if (this.$refs.mainMap != undefined) {
      			this.$refs.mainMap.goToLoc(temp);
      			this.$refs.mainMap.goToLoc(temp);
      		}
      	} catch (error) {
      		console.error(error);
      	}
      },
      /* 位置移动 */
      async handleTouchEnd(event) {
      	let that = this
      	try {
      		if (that.$refs.mainMap != undefined) {
      			let temp = that.$refs.mainMap.getCenterLatLng()
      			that.tempLat = temp.lat
      			that.tempLng = temp.lng
      
      			/* 拼装请求 */
      			let loc = {
      				lon: temp.lng,
      				lat: temp.lat,
      				ver: 1
      			}
      			const url = "https://api.tianditu.gov.cn/geocoder"
      			const data = {
      				postStr: JSON.stringify(loc),
      				type: 'geocode',
      				tk: that.tk
      			}
      			uni.request({
      				url: url,
      				data: data,
      				method: 'GET',
      				success: (res) => {
      					const temp = res.data.result
      					that.tempLocTxt = temp.formatted_address
      				},
      				fail: (err) => {
      					console.log(err);
      				}
      			});
      		}
      	} catch (e) {
      		console.log(e);
      	}
      },
      /* 定位修改 */
      upLocation() {
      	this.locLng = this.tempLng == null ? this.locLng : this.tempLng
      	this.locLat = this.tempLat == null ? this.locLat : this.tempLat
        this.xcddPos = `${this.locLng},${this.locLat}`
      	this.xcddTxt = this.tempLocTxt == '' ? this.xcddTxt : this.tempLocTxt
      	this.showMap = false
      },
      /* 定位取消 */
      cancel() {
        this.tempLat = this.locLat
        this.tempLng = this.locLng
        this.tempLocTxt = this.xcddTxt
      	this.showMap = false
      },
			/* 字典 */
			getDic(req) {
				getDateDic(req).then(res => {
					if (req.type == 'PROJECT_STAGE') {
						this.jsztList = [res]
					}
				})
			},
			getDic2() {
				getDateDic({
					status: 'Enabled',
					type: 'PROJECT_ZGZT'
				}).then(res => {
					this.zgDicList = res
				})
			},
      xcrzd() {
        this.xcrzShow = true
      },
			/* 弹出层选择器 */
			xmxcd(){
				this.xmxcdShow = true
			},
			groupChange(n){
				// console.log(n);
			},
			getXcproList(id){
				proList({pageNum:1,pageSize:100,xmid:id}).then(res => {
					this.xmxcList = res.list.filter(item => item.sfzgwc != 1)
				})
			},
			choiceThisItem(xp){
				this.xmxcdId = xp.id
				this.xmxcdTxt = xp.yjbh
				this.xmxcdShow = false
			},
			jumpToDetailXmxcd(xp){
				if(xp){
					uni.navigateTo({
						url: `/pages/me/xmxc/xmxcxq/index?id=${xp.id}&title=${xp.xmmc}`
					})
				}
			},
      choiceThisItem2(rz) {
        this.xcrzId = rz.id
        this.xcrzTxt = rz.createdTime.slice(0, 10)
        this.xcrzShow = false
      },
      jumpToDetail2(rz) {
        if(rz) {
          uni.navigateTo({
            url: `/pages/me/xcrzxq/index?id=${rz.id}&title=${rz.xmmc}`
          })
        }
      },
			getZt(xp){
				if(xp){
					let xx = this.zgDicList.find(item => item.code == xp.zgzt)
					if(xx){
						return xx.name
					}else{
						return ''
					}
				}
			},
			/* 选择器 */
			pro(){
				if (this.proTxt == '请选择') {
					this.proTxt = this.proListx[0][0].xmmc
				}
				this.proShow = true
			},
			jszt(){
				if (this.jsztTxt == '请选择') {
					this.jsztTxt = this.jsztList[0][0].name
				}
				this.jsztShow = true
			},
			zg(){
				if (this.zgTxt == '请选择') {
					this.zgTxt = this.zgList[0][0].name
				}
				this.zgShow = true
			},
			changePro(item){
				this.proStatus = 1
				let data = item.value
				this.proTxt = data[0].xmmc
				this.proId = data[0].id
				this.xmjdTxt = data[0].shjsjd
				let tempZt = data[0].shjszt
				let tempZtList = this.jsztList[0]
				let find = tempZtList.find(item => item.code == tempZt)
				if(find){
					this.jsztTxt = find.name
					this.jsztCode = find.code
				}
				getXmMessage({id:data[0].id}).then(res => {
					if(res){
						this.proDetail = res
					}
				})
				this.getXcproList(data[0].id)
				this.xmxcdTxt = '请选择'
				this.xmxcdId = null
				this.xcddTxt = ''
				this.questionList = [
					{content : ''}
				]
				this.proShow = false
			},
			changeJszt(item){
				this.jsztStatus = 1
				let data = item.value
				this.jsztTxt = data[0].name
				this.jsztCode = data[0].code
				this.jsztShow = false
			},
			changeZg(item){
				this.zgStatus = 1
				let data = item.value
				this.zgTxt = data[0].name
				this.zgValue = data[0].value
				this.zgShow = false
			},
			proCancel() {
				if (this.proStatus == 0) {
					this.proTxt = '请选择'
				}
				this.proShow = false
			},
			jsztCancel() {
				if (this.jsztStatus == 0) {
					this.jsztTxt = '请选择'
				}
				this.jsztShow = false
			},
			zgCancel() {
				if (this.zgStatus == 0) {
					this.zgTxt = '请选择'
				}
				this.zgShow = false
			},
			getDefaultPro() {
				if (this.proListx[0]) {
					let index = this.proListx[0].findIndex(obj => obj.xmmc === this.proTxt);
					return index
				} else {
					return
				}
			},
			getDefaultJszt() {
				if (this.jsztList[0]) {
					let index = this.jsztList[0].findIndex(obj => obj.name === this.jsztTxt);
					return index
				} else {
					return
				}
			},
			getDefaultZg() {
				if (this.zgList[0]) {
					let index = this.zgList[0].findIndex(obj => obj.name === this.zgTxt);
					return index
				} else {
					return
				}
			},
			
			/* 存在问题增加 */
			addQuestion(){
				this.questionList.push({
					content:''
				})
			},
			deleteQuestion(index){
				if(this.questionList.length==1){
					uni.$u.toast('至少保留一条问题记录')
					return
				}
				this.questionList.splice(index,1)
			},
			
			/* 图片上传 */
			getFile() {
				let that = this
				window.croods.customPlugin({
					action: 'ServicePermissionPlugin.uploadMaterial',
					params: {
						reason: "获取相机、文件权限",
						imgCount: "1",
						uploadTitle: "请选择上传方式",
						uploadType: "4",
					},
					success: function(res) {
						let tfile = res.files[0]
						if (res.flag) {
							let temp = tfile.file.replace(/[\r\n]/g, "")
							let url = 'data:' + tfile.type + ';base64,' + temp
							tfile.url = url
							that.fileTempList.push(tfile)
						} else {
							that.$u.toast('格式错误，上传失败');
						}
					},
					fail: function(msg) {
						if (msg != null || msg != undefined) {
							that.$u.toast(msg);
						} else {
							that.$u.toast('照片上传失败');
						}
					}
				});
			},
			removeItem(index) {
				this.fileTempList.splice(index, 1);
			},
			async getIds() {
				let that = this
				let fj = '';
				let promises = that.fileTempList.map(async item => {
					let temp = {
						bizType: "app",
						file: item.file,
						fileName: item.name,
						fileType: item.type
					}
					let res = await uploadFile(temp);
					if (res != undefined || res != null) {
						fj += res.id + ';'
					}
				})
				await Promise.all(promises);
				return fj;
			},

			/* 重置 */
			reset() {
        if(this.loading || this.submited) {
          return
        }
				this.proTxt = '请选择'
				this.jsztTxt = '请选择'
				this.proId = null
				this.proDetail = null
				this.jsztCode = ''
				this.questionList = [
					{content : ''}
				]
				this.xmjdTxt = ''
				this.xcddTxt = ''
				this.fileNum = null
				this.fileTempList = []
				this.zgTxt = '请选择'
				this.zgValue = null
				this.xmxcList = []
				this.xmxcdTxt = '请选择'
				this.xmxcdId = null
        this.xcddPos = ''
        this.xcry = ''
        this.xcrzTxt = '请选择'
        this.xcrzShow = false
        this.xcrzId = null
        this.xcrzList = []
			},

			/* 提交 */
			async submit() {
        if(this.loading || this.submited) {
          return
        }
        this.loading = true
				let that = this
        let fj;
        try {
          fj = await that.getIds()
        } catch (err) {
          console.log(err)
          that.$u.toast('附件上传失败')
        } finally {
          this.loading = false
        }
				that.fileNum = fj.slice(0, -1)

				/* 项目 */
				if (that.proTxt == '请选择' || that.proTxt == '') {
					that.$u.toast('请选择项目')
          this.loading = false
					return
				}
				if (that.proId==null){
					that.$u.toast('未获取到项目ID，请重新选择项目')
          this.loading = false
					return
				}

				/* 建设状态 */
				if (that.jsztTxt == '请选择' || that.jsztTxt == '') {
					that.$u.toast('请选择建设状态')
          this.loading = false
					return
				}
				
				/* 关联项目巡查单 */
				/* if(that.xmxcdId==null){
					that.$u.toast('请关联项目巡查单')
					return
				} */
			

				/* 项目进度 */
				if ((that.xmjdTxt + '').trim() == '') {
					that.$u.toast('请填写项目进度')
          this.loading = false
					return
				}
        if(!(/^\d+$/.test(that.xmjdTxt) && that.xmjdTxt >=0 && that.xmjdTxt <= 100)) {
          that.$u.toast('项目进度请填写0-100的整数')
          this.loading = false
          return
        }
				
				/* 巡查地点 */
				if (that.xcddTxt.trim() == '') {
					that.$u.toast('请填写巡查地点')
          this.loading = false
					return
				}
				
				/* 存在问题 */
				const filteredList = that.questionList.filter(item => item.content.trim() !== '')
				// if(filteredList.length==0){
				// 	that.$u.toast('请填写存在问题')
				// 	return
				// }
				/* 现场照片附件 */
				if (that.fileTempList.length == 0) {
					that.$u.toast('现场照片未上传')
          this.loading = false
					return
				}
        /* 是否整改 */
        if(that.xmxcdId != null && that.zgValue==null){
        	that.$u.toast('请选择整改是否完成')
          this.loading = false
        	return
        }

				let req = {
					shjsjd:that.xmjdTxt,
					shjszt:that.jsztCode,
					xcdd:that.xcddTxt,
					xmid:that.proId,
					zp:that.fileNum.replace(/\s/g,''),
					details:filteredList,
					projectRectificationProcessId:that.xmxcdId,
					sfzgwc:that.zgValue,
          lngLatCenter: that.xcddPos,
          xcry: that.xcry,
				}
        if(that.planId) {
          req.planId = that.planId
        }

				postXcrz(req).then(res => {
					if(res && (res.msg == '200' || res.msg == 200)){
            this.submited = true
						that.$u.toast('提交成功');
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
						}, 1000);
					} else {
						that.$u.toast('提交失败');
					}
				}).finally(() => {
          this.loading = false
        })
			}
		}
	}
</script>

<style scoped>
	.main {
		margin-top: 16px;
		background-color: #FAFAFA;
	}

	.x {
		font-size: 14px;
		color: #999999;
		font-family: 'PingFang SC';
	}
	.ayxzzz{
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-radius: 5px;
		border: 1px solid #dadbde;
		background-color: #FFF;
		padding: 6px 9px;
		margin: 10px 16px 0px 14px;
	}
  .opt {
    margin: 10px 16px 0px 14px;
  }
  .short {
    width: 50%;
  }
</style>